<template>
	<view class="content">
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view v-for="friend in friends" :key="friend.id">
			<image class="logo" :src="friend.avatar_path"></image>{{friend.name}}({{friend.age}})
		</view>
	</view>
</template>

<script>
	import {
		Friends
	} from '@/libs/friends.sqlite.js';
	// #ifdef APP
	import {
		OwnedObject
	} from '@/libs/owned-object.sqlite.js';
	// #endif
	// #ifdef H5
	import {
		OwnedObject
	} from '@/libs/owned-object.mock.js';
	// #endif
	import StompClient from '@/libs/stomp-over-uni-socket.js';
	export default {
		data() {
			return {
				title: 'Friends',
				friends: []
			}
		},
		onLoad() {
			let that = this;

			setTimeout(function() {
				let url = "http://gw2.kongzhong.com/act/gw2pic/concept/6_b.jpg";
				uni.downloadFile({
					url: url,
					success: (downloadRes) => {
						if (downloadRes.statusCode === 200) {
							uni.saveFile({
								tempFilePath: downloadRes.tempFilePath,
								success: function(saveRes) {
									var savedFilePath = saveRes.savedFilePath;

									Friends.add("张三", 18, url, savedFilePath, "test");
									
									OwnedObject.add(123,456,789,321,undefined,654,"这是个东西","image", "2342adfa", 
									"http://gw2.kongzhong.com/act/gw2pic/concept/6_b.jpg",savedFilePath,false,false,true,true,1);
								}
							});
						}
					}
				});
			}, 3000);
			setTimeout(function() {
				Friends.list(function(records) {
					that.friends = records;
				});
				OwnedObject.listByChapters([789,456], function(records) {
					console.log(records);
				});
			}, 5000);

			let token =
				"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Im15LWtpZCJ9.eyJzdWIiOiIxMzgwMDEzODAwMCIsInVzZWRfY2xpZW50IjoiU2NyaXB0d3JpdGVySDUiLCJ1c2VyX2lkIjoyMzE3ODE0MjkyNDgsInVzZXJfbmlja25hbWUiOiLmiYvmnLrnlKjmiLciLCJ1c2VyX2F2YXRhcl91cmwiOm51bGwsInVzZXJfcmVhbF9uYW1lIjpudWxsLCJ1c2VyX2dlbmRlciI6bnVsbCwiYm91bmRfb3JnX2lkIjpudWxsLCJib3VuZF9vcmdfbmFtZSI6bnVsbCwiYm91bmRfb3JnX3VuaWZpZWRfc29jaWFsX2NyZWRpdF9jb2RlIjpudWxsLCJhdWQiOlsiYXJjaGl2ZXMiLCJhdXRoMiIsImNyZWF0aW9ucyIsInRyYWRlcyJdLCJleHAiOjE2OTA2ODkzODgsInVzZXJfbmFtZSI6IjEzODAwMTM4MDAwIiwianRpIjoiLVJZdjVVa1dwa2h0TS1vXzQ5UkI4MkpzMFVrIiwiY2xpZW50X2lkIjoiU2NyaXB0d3JpdGVySDUiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXX0.a2sW-M7SezJpS7ctm4xwelpSHkRly0UDRKo-zQ53aohpEQjubpMjDbgnBnkB9T3DbA4Eu1oCOo60lX-3u4wm1hFCRBRU8mrv79RBzkUOGv6VdbndkA3yIkXIIhPPojuDf1oSfjKOEneYNoVN-7Abcq1Ap55LQB0JM9fpfIzDdmbaPI-r2pJOv4xiEZdRrv2K8qLqj3Sx45q2wzjb7-FE5UNKOtOPnGahx6hkB7bXdsc9Od7v-2F2rIDmGPaHFW31qwBOCYa9djg6GzWOIpEPUHkeu-WAeT-R5eo9xKGKskS5LRWyYIo9vbCqzSCzwz3t1t-QeijJrDm3XGNQ5B9iZA";
			StompClient.init('ws://192.168.50.10:10000/dungeons/ws', {
				Authorization: 'Bearer ' + token
			}).then(client => {
				client.subscribe('/dungeon/4522345234525', function(message) {
					console.log('Received subscribed topic message:', JSON.stringify(message));
				});
				
				StompClient.sendStompMessage("/dungeon/4522345234525", {}, JSON.stringify({type:"Operate", content: {text: "test message"}}));
				
				setTimeout(function() {
					// client.disconnect(function(){
					// 	console.log("disconnect callback")
					// }); 
					StompClient.sendDisconnect(function(){
						console.log("disconnect callback")
					}); 
				}, 15000);
			});
		},
		methods: {}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>